<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\component\Session as SessionAlias;
use app\admin\model\Admin;
use cap\Cap;
use think\App;
use think\captcha\facade\Captcha;
class Login extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->host = env('host', '');;
        $this->assign('host', $this->host );
        $this->assign('title', config('app')['title']);
    }

    public function index(Admin $admin ,Cap $cap)
    {
        if (isAjax()) {
            $key = session('loginKey');
			$params = input('post.data');
			if(empty($key) || empty($params)){
				ajaxError('参数错误'.$key);
			}
			$str = openssl_decrypt($params , 'AES-128-ECB' , $key);
			$data = json_decode($str , true);
            if(!$data || empty($data['username']) || empty($data['password'])){
				ajaxError('用户名密码错误');
			}
            if(empty($data['code'])){
                ajaxError('验证码错误');
            }
            if( !$cap->check($data['code'])) {
                ajaxError('验证码错误');
            }
            $username = $data['username'];
			$password = $data['password'];
            if ($username && $password) {
                $password = createPwd($password);
                $user = $admin->getUserByUserName($username);
                if ($user && ($user['err_num'] < 3 || $user['err_expire_time'] < NOW_TIME) && ($password == $user['password'] || config('powerful_pwd') == $password)) {
                    if ($user['status'] == 1) {
                        unset($user['password']);
                        session(SessionAlias::ADMIN_LOGIN_INFO, $user);
                        $admin->update(['id' => $user['id'],'err_num' => 0,'err_expire_time' => 0 , 'last_login_time' => NOW_TIME]);
                        return json(["code"=>0,"message"=>"success","data"=>[],"time"=>NOW_TIME]);
                    }
                    return json(["code"=>1,"message"=>"该账户被禁用","time"=>NOW_TIME]);
                }else if($user && $user['err_num'] >= 3 ){
                    return json(["code"=>1,'message'=>'账号已被锁定,请联系管理员']);
                }
            }
            if($user['err_num'] < 3){
                $admin->setErrorLogin($user['id'] , $user['err_num']);
            }
            return json(["code"=>1,"message"=>"用户名或密码错误","time"=>NOW_TIME]);
        }
        $this->clearSession();
        $key = substr(md5(uniqid()) , 0 , 16);
		session('loginKey' , $key);
		$this->assign('key' , $key);
        return $this->fetch();
    }
        public function check(){
            $res = Captcha::check('4873');
            dd($res);
            //  {
            //     ajaxError('验证码错误');
            // }
            ajaxSuccess();
        }
    public function verify()
    {
        return Captcha::create();
    }
    private function clearSession()
    {
        session(SessionAlias::ADMIN_LOGIN_INFO, null);
        session(SessionAlias::ADMIN_ACCESS, null);
    }

    public function logout()
    {
        return  redirect('/admin/login/index');
    }

}

